<?php
namespace app\home\controller;

use think\Request;
use think\Db;
use app\common\logic\Upload;
use app\common\logic\Seller as Lseller;

/**
 * Operation Controller
 */
class Operation extends Common
{
	/**
	 * 运营管理页
	 */
	public function index()
    {
        $userInfo = $this->getLoginUserInfo();

        $sellerIds = '';
        switch ($userInfo['session_role']) {
            case 1:
            case 2:
                $sellers = Db::name('seller')
                    ->field('id')
                    ->where(['agency' => $userInfo['session_id']])
                    ->select();

                $sellerIds = implode(',', array_column($sellers, 'id'));
                break;
            case 3:
                die('权限不足');
        }

        $time = input('time', 1);

        $starTime = input('start_time');
        $endTime = input('end_time');

        if (empty($starTime) && empty($endTime)) {
            switch ($time) {
                case 1:
                    $beginTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
                    $endTime = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
                    break;
                case 2:
                    $timestamp = time();
                    $beginTime = strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp)));
                    $endTime =strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1;
                    break;
                case 3:
                    $beginTime = mktime(0, 0, 0, date('m'), 1, date('Y'));
                    $endTime = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
                    break;
            }
        } else {
            if (empty($starTime)) {
                $starTime = 0;
            }
            $beginTime = strtotime($starTime);
            $endTime = strtotime($endTime);
        }

        $where = [
            'create_time' => ['between', [$beginTime, $endTime]],
            'status' => 2,
            'is_delete' => 1,
            'sid' => ['in', $sellerIds]
        ];

        $orders = Db::name('leaseorder')->where($where)->select();

        // 交易笔数
        $orderCount = count($orders);

        // 交易总额
        $orderAmount = array_sum(array_column($orders, 'real_amount'));

        // 商户佣金
        $sellerBrokerage = array_sum(array_column($orders, 'seller_brokerage'));

        // 代理商佣金
        $agentBrokerage = array_sum(array_column($orders, 'agent_brokerage'));

        // 总佣金
        $allBrokerage = $sellerBrokerage + $agentBrokerage;

        // 地推统计
        $agentCount = Db::name('agent')->where([
            'addtime' => ['between', [$beginTime, $endTime]],
            'belong_agent' => $userInfo['session_id']
        ])->count();
        $allAgentCount = Db::name('agent')->where([
            'belong_agent' => $userInfo['session_id']
        ])->count();

        // 商户统计
        $sellerCount = Db::name('seller')->where([
            'create_time' => ['between', [$beginTime, $endTime]],
            'agency' => $userInfo['session_id']
        ])->count();
        $allSellerCount = Db::name('seller')->where([
            'agency' => $userInfo['session_id']
        ])->count();

        // 用户统计
        $userCount = Db::name('user')->where([
            'create_time' => ['between', [$beginTime, $endTime]]
        ])->count();
        $allUserCount = Db::name('user')->count();


        return $this->fetch('index', [
            'time' => $time,
            'agent_count' => $agentCount,
            'all_agent_count' => $allAgentCount,
            'seller_count' => $sellerCount,
            'all_seller_count' => $allSellerCount,
            'user_count' => $userCount,
            'all_user_count' => $allUserCount,
            'order_count' => $orderCount,
            'order_amount' => $orderAmount,
            'seller_brokerage' => $sellerBrokerage,
            'agent_brokerage' => $agentBrokerage,
            'all_brokerage' => $allBrokerage,
        ]);
    }
}

